// ==UserScript==
// @name         getPlaylist
// @namespace    http://tampermonkey.net/
// @version      2024-12-05
// @description  获取bilibili播放列表
// @author       zxywonder
// @include     *://*bilibili*/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=bilibili.com
// @grant        unsafeWindow
// @run-at       document-idle
// @downloadURL https://gitee.com/zxywonder/js_script/blob/master/bilibili_getPlaylist.js
// @updateURL https://gitee.com/zxywonder/js_script/blob/master/bilibili_getPlaylist.js
// ==/UserScript==

(function () {
    'use strict';

    function get_playlist() {
        let content = '';
        var cur_url = window.location.href
        if (cur_url.startsWith('https://www.bilibili.com/video/')) {
            console.log(cur_url)

            var datas = window.document.querySelector('div.video-pod__list')
            if (datas) {
                datas.children.forEach(function (key) {
                    content += 'https://www.bilibili.com/video/' + key.getAttribute('data-key') + '/\n';
                })
            }
            else {
                window.alert("可以直接复制连接使用哦！");
            }
        }
        else {
            var fj_list = window.document.querySelector('div.eplist_ep_list_wrapper__Sy5N8')
            if (fj_list) {
                fj_list.lastChild.children.forEach(function (key) {
                    let a_node = key.querySelector('a');
                    if (a_node) {
                        content += a_node.href + '/\n';
                    }
                })
            }
        }

        if (content.length > 0) {
            console.log(content)
            unsafeWindow.navigator.clipboard.writeText(content).then(() => window.alert("播放链接复制成功！"))
        }
    }

    let button = window.document.createElement('button');
    button.textContent = '获取播放列表';
    button.style.position = 'fixed';
    button.style.top = '60px';
    button.style.right = '10px';
    button.style.backgroundColor = 'green';
    button.style.color = 'white';
    button.style.padding = '10px';
    button.style.borderRadius = '5px';
    button.style.cursor = 'pointer';
    button.style.zIndex = '99';
    button.addEventListener('click', get_playlist);

    let body = window.document.querySelector('body');
    body.insertBefore(button, body.firstChild);



    // Your code here...
})();